﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Emoney.UserTraining.Comm
{
    public class OTO
    {
        public OTO()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        public static int? ToInt(object obj, int? v)
        {
            try
            {
                v = int.Parse(obj.ToString());
            }
            catch { }
            return v;
        }

        public static int ToInt(object obj, int v)
        {
            try
            {
                v = int.Parse(obj.ToString());
            }
            catch { }
            return v;
        }

        public static long ToLong(object obj, long v)
        {
            long rel = v;
            try
            {
                rel = long.Parse(obj.ToString());
            }
            catch { }
            return rel;
        }


        public static DateTime? ToDate(object obj, DateTime? v)
        {
            try
            {
                v = DateTime.Parse(obj.ToString());
            }
            catch { }
            return v;
        }

        public static DateTime ToDate(object obj, DateTime v)
        {
            try
            {
                v = DateTime.Parse(obj.ToString());
            }
            catch { }
            return v;
        }

        public static decimal? ToDecimal(object obj, decimal? v)
        {
            try
            {
                v = decimal.Parse(obj.ToString());
            }
            catch { }
            return v;
        }

        public static decimal ToDecimal(object obj, decimal v)
        {
            try
            {
                v = decimal.Parse(obj.ToString());
            }
            catch { }
            return v;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj">数据源</param>
        /// <param name="a">除数</param>
        /// <param name="b">小数位</param>
        /// <param name="u">单位</param>
        /// <param name="r">替换值</param>
        /// <returns></returns>
        public static string ToDecimal(object obj, int a, int b, string u, string r)
        {
            try
            {
                r = (decimal.Parse(obj.ToString()) / a).ToString("f" + b) + u;
            }
            catch { }
            return r;
        }

        public static string ToStr(object obj)
        {
            string rel = String.Empty;
            try
            {
                rel = obj.ToString();
            }
            catch { }
            return rel;
        }

        public static string ToStr(object obj, string v)
        {
            string rel = "";
            try
            {
                rel = obj.ToString();
            }
            catch { }
            if (rel.Trim().Equals(""))
                rel = v;
            return rel;
        }


        public static string ToDateLine(object obj, int s)
        {
            string rel = "-";
            try
            {
                switch (s)
                {
                    case 1:
                        rel = DateTime.Parse(obj.ToString()).ToString("yyyy-MM-dd");
                        break;
                    default:
                        break;
                }
            }
            catch
            {
            }
            if (rel == "1901-01-01" || rel == "1900-01-01")
                rel = "-";
            return rel;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj">要格式化的值</param>
        /// <param name="n">小数位长度</param>
        /// <param name="v">异常返回值</param>
        /// <returns></returns>
        public static string ToDecLine(object obj, int n, string v)
        {
            string rel = "-";
            try
            {
                rel = decimal.Parse(obj.ToString()).ToString("F" + n);
            }
            catch
            {
            }
            if (rel == "0.00")
                rel = v;
            return rel;
        }

        public static string ToLongLine(object obj, string v)
        {
            string rel = "-";
            try
            {
                rel = long.Parse(obj.ToString()).ToString();
            }
            catch
            {
            }
            if (rel == "0")
                rel = v;
            return rel;
        }

        public static string ToDecLine(object obj, int n, string v, string dw)
        {
            string rel = "0.00";
            try
            {
                rel = decimal.Parse(obj.ToString()).ToString("F" + n);
            }
            catch
            {
            }
            decimal temp = 0;
            if (rel == decimal.Parse(temp.ToString()).ToString("F" + n))
                rel = v;
            else
                rel = rel + dw;
            return rel;
        }

        /// <summary>
        /// ConvertUnit
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string CoUt(object obj, int u, string s)
        {
            string rel = s;
            try
            {
                rel = OTO.ToDecimal(int.Parse(obj.ToString()) / u, 0).ToString("f2");
            }
            catch { }
            return rel;
        }

        public static string CoUt(object obj, int u, string s, int f)
        {
            string rel = s;
            try
            {
                rel = OTO.ToDecimal(int.Parse(obj.ToString()) / u, 0).ToString("f" + f);
            }
            catch { }
            return rel;
        }

        /// <summary>
        /// 红涨绿跌
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="s">异常返回</param>
        /// <param name="u">单位</param>
        /// <returns></returns>
        public static string RedGre(object obj, string s, string u)
        {
            string rel = s;
            try
            {
                decimal d = OTO.ToDecimal(obj, 0);
                if (d > 0)
                    rel = "<span class=\"red\">" + obj.ToString() + u + "</span>";
                else if (d == 0)
                    rel = "<span >" + s + "</span>";
                else
                    rel = "<span class=\"green\">" + obj.ToString() + u + "</span>";
            }
            catch { }
            return rel;
        }


        public static string PerRedGre(object obj, int n, string s, string u)
        {
            string rel = s;
            try
            {
                decimal d = OTO.ToDecimal(obj, 0);
                if (d > 0)
                    rel = "<span class=\"red\">" + d.ToString("f" + n) + u + "</span>";
                else if (d == 0)
                    rel = "<span >" + s + "</span>";
                else
                    rel = "<span class=\"green\">" + d.ToString("f" + n) + u + "</span>";
            }
            catch { }
            return rel;
        }

        /// <summary>
        /// zdf
        /// </summary>
        /// <param name="obj">要格式的数据</param>
        /// <param name="m">被整除</param>
        /// <param name="n">小数位长度</param>
        /// <param name="s">异常替换</param>
        /// <param name="u">单位</param>
        /// <returns></returns>
        public static string PerRedGre(object obj, int m, int n, string s, string u)
        {
            string rel = s;
            try
            {
                decimal d = OTO.ToDecimal(obj, 0);
                if (d > 0)
                    rel = "<span class=\"red\">" + (d / m).ToString("f" + n) + u + "</span>";
                else if (d == 0)
                    rel = "<span >" + s + "</span>";
                else
                    rel = "<span class=\"green\">" + (d / m).ToString("f" + n) + u + "</span>";
            }
            catch { }
            return rel;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public static string CutStr(string str, int n)
        {
            string rel = str;
            try
            {
                if (str.Length > n)
                    rel = str.Substring(0, n);
            }
            catch { }
            return rel;
        }

        /// <summary>
        /// 过滤HTML标签
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DelHtml(string str)
        {
            str = Regex.Replace(str, "<[^>]+>", "");
            return str;
        }

        /// <summary>
        /// 110107 式字符转化为日期 
        /// </summary>
        /// <param name="obj">被格式化字符</param>
        /// <param name="s">异常替换</param>
        /// <returns></returns>
        public static string ToDate(string obj, string s)
        {
            string v = s;
            try
            {
                v = obj.Substring(2, 2) + "/" + obj.Substring(4, 2);
            }
            catch { }
            return v;
        }

        public static string GetShortTime(DateTime dt)
        {
            string rel = "";

            if (dt.Date == DateTime.Now.Date)
                rel = dt.ToString("HH:mm");
            else
                rel = dt.ToString("MM-dd");
            return rel;
        }
    }
}
